-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[compiler-rt] [test] Add test for frame counter out of order. #154190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler-rt] [test] Add test for frame counter out of order. #154190
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Jesse Schwartzentruber (jschwartzentruber) ChangesAdd test for #148278. This was written with the aide of ChatGPT 5 and tested on Linux x86_64. Full diff: https://github.com/llvm/llvm-project/pull/154190.diff 1 Files Affected:
diff --git a/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp b/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp
new file mode 100644
index 0000000000000..7ed3ddd8b81ed
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Posix/asan_symbolize_script/anon_inline_wrong_frame_number.cpp
@@ -0,0 +1,73 @@
+// This test case checks for a bug where inlined functions and anonymous code
+// result in out-of-order stack frame numbers.
+
+// UNSUPPORTED: android
+// UNSUPPORTED: aarch64
+// UNSUPPORTED: darwin
+// UNSUPPORTED: ios
+
+// RUN: %clangxx_asan -O0 -g %s -o %t
+// RUN: %env_asan_opts=symbolize=0 not %run %t DUMMY_ARG > %t.asan_report 2>&1
+// RUN: %asan_symbolize --log-level debug --log-dest %t_debug_log_output.txt -l %t.asan_report > %t.asan_report_sym
+// RUN: FileCheck --input-file=%t.asan_report_sym %s
+
+#include <sys/mman.h>
+#include <unistd.h>
+#include <cstdint>
+#include <cstring>
+#include <cstdio>
+#include <cstddef>
+
+static void call_via_anon_page(void (*fn)()) {
+ const size_t pagesz = static_cast<size_t>(sysconf(_SC_PAGESIZE));
+ uint8_t *mem = static_cast<uint8_t*>(
+ mmap(nullptr, pagesz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
+ if (mem == MAP_FAILED) perror("mmap");
+
+#if defined(__x86_64__)
+ // x86_64: mov rax, imm64; call rax; ret
+ // 48 B8 <imm64> FF D0 C3
+ uint8_t stub[2 + 8 + 2 + 1] = {0x48, 0xB8};
+ std::memcpy(stub + 2, &fn, sizeof(fn)); // imm64
+ stub[2 + 8] = 0xFF; stub[2 + 9] = 0xD0; // call rax
+ stub[2 + 10] = 0xC3; // ret
+#else
+#error "unsupported platform"
+#endif
+
+ std::memcpy(mem, stub, sizeof(stub));
+ mprotect(mem, pagesz, PROT_READ | PROT_EXEC);
+
+ using Thunk = void (*)();
+ reinterpret_cast<Thunk>(mem)();
+
+ munmap(mem, pagesz);
+}
+
+__attribute__((always_inline)) static void inline3(char* p, size_t n) {
+ // out-of-bounds write to trigger ASan
+ p[n] = 42;
+}
+
+__attribute__((always_inline)) static void inline2(char* p, size_t n) { inline3(p, n); }
+__attribute__((always_inline)) static void inline1(char* p, size_t n) { inline2(p, n); }
+
+__attribute__((noinline)) static void top() {
+ char p[8];
+ inline1(p, 16); // deliberately OOB; inlined chain expands in symbolizer
+}
+
+int main() {
+ call_via_anon_page(top);
+ return 0;
+}
+
+// Check that the numbering of the stackframes is correct.
+
+// CHECK: AddressSanitizer: stack-buffer-overflow
+// CHECK-NEXT: WRITE of size
+// CHECK-NEXT: #0 0x{{[0-9a-fA-F]+}} in inline3
+// CHECK-NEXT: #1 0x{{[0-9a-fA-F]+}} in inline2
+// CHECK-NEXT: #2 0x{{[0-9a-fA-F]+}} in inline1
+// CHECK-NEXT: #3 0x{{[0-9a-fA-F]+}} in top
+// CHECK-NEXT: #4 0x{{[0-9a-fA-F]+}}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an XFAIL annotation (otherwise, this test will break the build as soon as it lands)
P.S. I think the JIT'ed code is the important part, the inlining just adds noise. When I remove the inlined functions, it still reproduces the wrong numbering:
|
fb3ee4c
to
1821833
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
1821833
to
6fd6f66
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch! Looks good to me.
Let me know if/when you'd like me to press the big merge button.
Thank you! Anytime is fine with me. |
@jschwartzentruber Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Add test for #148278. This was written with the aide of ChatGPT 5 and tested on Linux x86_64.